﻿using System;
using System.Collections.Specialized;
using System.Web.Mvc;
using ControleRepresentante.Logic.Models;
using ControleRepresentante.Logic.Repositories;
using ControleRepresentante.Logic.Statistics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WebSite.Controllers;

namespace ControleRepresentante.UnitTests.Controllers
{
    [TestClass]
    public class RelatorioVisitaControllerTests : CRUDControllerTestScenarioBase<RelatorioVisitaController, RelatorioVisita, IRelatorioVisitaRepository, StatisticsBase>
    {
        private Representante m_Representante;
        private Guid m_VendedorId;
        private Guid m_ClienteId;
        private static readonly DateTime Data = DateTime.Today;
        private const string Observacao = "drgdfgsd fgsdf gsdf gsdfg sdfgsd f";
        private const RelatorioVisitaJolitexOcorrencia Ocorrencia = RelatorioVisitaJolitexOcorrencia.Estocado;

        protected override RelatorioVisitaController CreateController(bool isRepresentante = true)
        {
            this.LoginAsAdmin();
            Guid representanteId = this.CreateRepresentante("repre", true, "repreuser");
            m_Representante = RepositoryProvider.GetRepository<IRepresentanteRepository>().GetModelById(representanteId);
            this.CreateRepresentante("otherrepre", false, "other");
            this.Logout();
            this.LoginAs("repreuser");

            m_VendedorId = this.CreateVendedor(new Vendedor { Nome = "vendedor" });
            Cliente cliente = new Cliente
            {
                RazaoSocial = "cliente",
                NomeFantasia = "cliente",
                Vendedor = new Vendedor {Id = m_VendedorId}
            };
            m_ClienteId = this.CreateCliente(cliente);

            if (!isRepresentante)
            {
                this.Logout();
            }
            return new RelatorioVisitaController(RepositoryProvider, SessionConfiguration, ControllerExtensionProvider);
        }

        protected override RelatorioVisita CreateModel()
        {
            return new RelatorioVisita
            {
                Observacao = String.Empty,
                Vendedor = RepositoryProvider.GetRepository<IVendedorRepository>().GetModelById(m_VendedorId),
                Cliente = RepositoryProvider.GetRepository<IClienteRepository>().GetModelById(m_ClienteId),
                Representante = m_Representante
            };
        }

        protected override ActionResult Create(RelatorioVisitaController controller)
        {
            return controller.Create(Data, m_ClienteId, m_VendedorId, Observacao, CreateFormCollection());
        }

        private static FormCollection CreateFormCollection()
        {
            NameValueCollection nameValueCollection = new NameValueCollection
            {
                {"ocorrencia", Ocorrencia.ToString()}
            };
            return new FormCollection(nameValueCollection);
        }

        protected override void VerifyModel(RelatorioVisita model)
        {
            Assert.AreEqual(m_ClienteId, model.Cliente.Id);
            Assert.AreEqual(m_VendedorId, model.Vendedor.Id);
            Assert.AreEqual(Data, model.Data);
            Assert.AreEqual(Observacao, model.Observacao);
            Assert.IsNotNull(model.RelatorioVisitaJolitex);
            Assert.AreEqual(Ocorrencia, model.RelatorioVisitaJolitex.Ocorrencia);
        }

        protected override ActionResult CreateEmptyFields(RelatorioVisitaController controller)
        {
            return controller.Create(Data, m_ClienteId, m_VendedorId, Observacao, CreateFormCollection());
        }

        protected override void VerifyModelEmptyFields(RelatorioVisita model)
        {
        }

        protected override ActionResult CreateDuplicatedModel(RelatorioVisitaController controller, RelatorioVisita model)
        {
            return controller.Create(Data, m_ClienteId, m_VendedorId, Observacao, CreateFormCollection());
        }

        protected override void VerifyDuplicatedModel(RelatorioVisita originalModel, RelatorioVisita duplicatedModel)
        {
        }

        protected override ActionResult Edit(RelatorioVisitaController controller)
        {
            return controller.Edit(Data, m_ClienteId, m_VendedorId, Observacao, CreateFormCollection());
        }

        protected override ActionResult EditEmptyFields(RelatorioVisitaController controller)
        {
            return controller.Edit(Data, m_ClienteId, m_VendedorId, Observacao, CreateFormCollection());
        }

        protected override ActionResult EditDuplicatedModel(RelatorioVisitaController controller, RelatorioVisita modelToDuplicate)
        {
            return controller.Edit(Data, m_ClienteId, m_VendedorId, Observacao, CreateFormCollection());
        }

        protected override bool ShouldRunTestCreateDuplicatedField { get { return false; } }
        protected override bool ShouldRunTestCreateEmptyFields { get { return false; } }
        protected override bool ShouldRunTestEditDuplicatedFields { get { return false;  } }
        protected override bool ShouldRunTestEditEmptyFields { get { return false; } }

        [TestMethod]
        public void TestChangeVendedor()
        {
            this.LoginAsAdmin();
            this.CreateRepresentante("repre", true, "u");
            this.Logout();
            this.LoginAs("u");
            Guid vendedorId = this.CreateVendedor("vendedor");
            Guid vendedor2Id = this.CreateVendedor("vendedor2");
            Guid clienteId = this.CreateCliente("cliente", vendedorId);
            Guid relatorioVisitaId = this.CreateRelatorioVisita(DateTime.Now, vendedorId, clienteId, "observacao", Ocorrencia);
            RelatorioVisitaController relatorioVisitaController = new RelatorioVisitaController(RepositoryProvider, SessionConfiguration, ControllerExtensionProvider);
            relatorioVisitaController.Edit(relatorioVisitaId);
            relatorioVisitaController.Edit(DateTime.Now, clienteId, vendedor2Id, "observacao", CreateFormCollection());
            IRelatorioVisitaRepository relatorioVisitaRepository = RepositoryProvider.GetRepository<IRelatorioVisitaRepository>();
            RelatorioVisita relatorioVisita = relatorioVisitaRepository.GetModelById(relatorioVisitaId);
            Assert.AreEqual(vendedor2Id, relatorioVisita.Vendedor.Id);
        }

        [TestMethod]
        public void TestChangeCliente()
        {
            this.LoginAsAdmin();
            this.CreateRepresentante("repre", true, "u");
            this.Logout();
            this.LoginAs("u");
            Guid vendedorId = this.CreateVendedor("vendedor");
            Guid clienteId = this.CreateCliente("cliente", vendedorId);
            Guid cliente2Id = this.CreateCliente("cliente2", vendedorId);
            Guid relatorioVisitaId = this.CreateRelatorioVisita(DateTime.Now, vendedorId, clienteId, "observacao", Ocorrencia);
            RelatorioVisitaController relatorioVisitaController = new RelatorioVisitaController(RepositoryProvider, SessionConfiguration, ControllerExtensionProvider);
            relatorioVisitaController.Edit(relatorioVisitaId);
            relatorioVisitaController.Edit(DateTime.Now, cliente2Id, vendedorId, "observacao", CreateFormCollection());
            IRelatorioVisitaRepository relatorioVisitaRepository = RepositoryProvider.GetRepository<IRelatorioVisitaRepository>();
            RelatorioVisita relatorioVisita = relatorioVisitaRepository.GetModelById(relatorioVisitaId);
            Assert.AreEqual(cliente2Id, relatorioVisita.Cliente.Id);
        }
    }
}
